package com.example.schoolexam2.service.impl;

import com.example.schoolexam2.dto.StudentDto;
import com.example.schoolexam2.entity.Student;
import com.example.schoolexam2.entity.StudentTemporaryBean;
import com.example.schoolexam2.mapper.StudentMapper;
import com.example.schoolexam2.service.StudentService;
import com.example.schoolexam2.utils.JwtUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static com.example.schoolexam2.utils.InformationToolClass.Studentconversion;

@Service
public class StudentInfoimpl implements StudentService {
    @Autowired
    StudentMapper studentMapper;
    public int studentPhoto(MultipartFile file){

        return 0;
    }

    //excel学生表导入
    @Override
    public int studentIncreases(MultipartFile file, int RequestType) throws Exception {
        Workbook workbook;
        try {
            if (RequestType == 0) {
                workbook = new HSSFWorkbook(file.getInputStream());
            } else if (RequestType == 1) {
                workbook = new XSSFWorkbook(file.getInputStream());
            } else {
                return 0;
            }
            Sheet sheetAt = workbook.getSheetAt(0);
            int lastRowNum = sheetAt.getLastRowNum();//获得所有行
            List<StudentTemporaryBean> list = new ArrayList<>();
            for (int i = 2; i <= lastRowNum; i++) {
                StudentTemporaryBean studentTemporaryBean = new StudentTemporaryBean();
                Row row = sheetAt.getRow(i);
                if (row != null) {
                    studentTemporaryBean = Studentconversion(row);
                }
                list.add(studentTemporaryBean);
            }

            int res = 0;
            for (int i = 0; i < list.size(); i++) {
                res = res + studentMapper.studentIncreases(list.get(i));
            }
            return res;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int studentIncrease(StudentDto student) {
        int res = 0;
        try {
            if (student.getPassword().isEmpty()) {
                return 0;
            } else {
                String password = student.getPassword();
                String newpassword = JwtUtils.addM(password);
                student.setPassword(newpassword);
            }
            res = studentMapper.studentIncrease(student);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    @Override
    public int studentDelete(String studentId, int isUser) {
        int rse = 0;
        try {
            Student student = studentMapper.studentPersonal(studentId);
            if (student == null) {
                return 0;
            } else if (student.getIsUser() == 1) {
                rse = studentMapper.studentDeleteTrue(studentId);
            } else {
                rse = studentMapper.studentDelete(studentId, isUser);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rse;
    }

    @Override
    public int studentUpdate(Student student) {
        int res = 0;
        try {
            res = studentMapper.studentUpdate(student);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    @Override
    public List<Student> studentPersonal(String studentId) {
        List<Student> list = new ArrayList<>();
        try {
            Student student = studentMapper.studentPersonal(studentId);
            if (student.getIsUser() == 0) {
                list.add(student);
                return list;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Student> studentFuzzy(String fuzzy) {
        Student student;
        List<Student> listAll = new ArrayList();
        try {
            List<Student> list = studentMapper.studentFuzzy(fuzzy);
            for (int i = 0; i < list.size(); i++) {
                student = list.get(i);
                if (student.getIsUser() == 0) {
                    listAll.add(student);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return listAll;
    }

    @Override
    public List<Student> studentAll(String classId) {
        Student student;
        List<Student> listAll = new ArrayList();
        try {
            List<Student> list = studentMapper.studentAll(classId);
            for (int i = 0; i < list.size(); i++) {
                student = list.get(i);
                if (student.getIsUser() == 0) {
                    listAll.add(student);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return listAll;
    }

}
